#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>

char buf[128] = {"hahahaha"};

//线程函数
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
void *threadFun(void *arg)
{
    //pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
    
    int i = 10;
    //强转
    int *p = (int *)arg;
    while(i--)
    {
        (*p)++;
        sleep(1);
        printf("hello,%d\n",*p);
        if(i == 2)
        {
            //malloc,或者全局变量
             //char buf[128] = {"hahahaha"};//巧合正确
             pthread_exit(buf);
           // pthread_setcancelstate(0,NULL);
        }
    }
}
 //int 
//int pthread_setcanceltype(int type, int *oldtype);
//pthread_join(pthread_t thread, void **retval);

int main()
{
    int val = 0;
    pthread_t tid;
    //创建线程
    pthread_create(&tid,NULL,threadFun,(void *)&val);
    printf("in main thread ID = %ld\n",tid);

    char *pret = NULL;
    pthread_join(tid,(void **)&pret);

    printf("in main:val = %d\n",val);
    printf("pret = %s\n",pret);


    return 0;
}